<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
if ($_GET['syn_type'] == 'admin') {
	
	session_start();
	$_SESSION['AName'] = 'administrator';
	$_SESSION['AID'] = 1;
	$_SESSION['Power'] = 65535;
	$_SESSION['APwd'] = 'd7d917e2e2f7bbea96fecbce17357b91';
}

require('../includes/init.php');
require('./classes/control/control_faq_new.php');

class Faq_New {
	
	var $faq_new_c;
	var $special_s;//需要替换的数组
	var $special_null;//替换以后的数组
	var $webType;//网站类型
	
	/**
	* @ 构造函数,实例化CommentControl
	* @ __construct
	*/
	public function __construct() {
		
		$this->faq_new_c = new FaqNewControl();
		$this->webType = empty($_REQUEST['web_type']) ? 1 : (int) $_REQUEST['web_type'];
		
		$this->special_s = array(0 => "'",1 => '"',2 => '$',3 => '@',4 => '.',5 => '&nbsp;',6 => '%',7 => '&',8 => '!',9 => ')',10 => '(',
		11 => '*',12 => '+',13 => '=',14 => '#',15 => '~',16 => ',',17 => '^',18 => '--',19 => 'ʻ',20 => 'ʼ',21 => '&',22 => 'amp;',
		23 => '“',24 => '”',25 => 'ǃ',26 => '/',27 => '!',28 => '\\',29 => '?',30 => '”',31 => '“',32 => '”',33 => '？',34 => '、',35 => '’',36 => '）'); 
		
		$this->special_null = array(0 => '',1 => '',2 => '-',3 => '-',4 => '',5 => '-',6 => '-',7 => '-',8 => '-',9 => '',10 => '',
		11 => '-',12 => '-',13 => '-',14 => '-',15 => '-',16 => '',17 => '-',18 => '-',19 => '-',20 => '-',21 => '-',22 => '-'
		,23 => '-',24 => '-',25 => '',26 => '-',27 => '',28 => '',29 => '',30 => '',31 => '',32 => '',33 => '',34 => '',35 => '',36 => '');	
	}
	
	/**
	* 函数del_template ,根据sql语句得到数据 删除指定的链接
	* @param  NULL
	* @return  $sql
	*/
	function del_template($sql = '', $type = '') {
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		$result = $GLOBALS['db']->query($sql);
		
		while ($row = $GLOBALS['db']->fetch_array($result)) {
			
			if ($type == 'del_directory' ) {
				
				$url = str_replace(array('/index.html', '/'.$temInfo['webDomain'].'/httpdocs/'), array('', ''), $row['url']);
				$new_url = str_replace('/'.$temInfo['webDomain'].'/httpdocs/', '', $temInfo);
				
				if ( !empty($url) && (substr_count('/'.$temInfo['webDomain'].'/httpdocs/',$row['url']) > 0 ) && !empty($new_url)) {
				
					delDirAndFile('/var/www/website/cms.wondershare.com.br/httpdocs/vhosts/'.str_replace(array('/index.html'),
								  array(''), $row['url']));
				}
			} else {
				
				if (!empty($row['url'])) {
					@unlink('/var/www/website/cms.wondershare.com.br/httpdocs/vhosts'.$row['url']);
				}
			}
		}
		
		unset($temInfo);
	
	}
	/**
	* 函数replace_str ,返回替换之后的字符串
	* @param  NULL
	* @return  string
	*/
	function replace_str($str) {
		
		$str = strtolower(trim(str_replace($this->special_s, $this->special_null,$str)));
		
		$str =  str_replace(array(' ','----','---','--'), array('-','-','-','-'), preg_replace('/\s/is', ' ', $str));
		return $str;
	}

	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	function index() {
	
		$url_info = $new_info = $category_info = array();
		$page = !empty($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1;
		$new_title = !empty($_POST['new_title']) ? trim($_POST['new_title']) : urldecode(trim($_GET['new_title']));
		$page_size = 20;
		
		//搜索以及分页
		if  ( $_POST['search'] || !empty($_REQUEST['url_page']) ) {
			
			$search_array = array (
									'type_id'  =>  trim($_REQUEST['new_type']),
									'reason_id'  =>  trim($_REQUEST['new_reason_id']),
									'resolve_status'  =>  trim($_REQUEST['new_status']),
									'id'  =>  (int)($_REQUEST['new_id']),
									'p_cbsid'  =>  (int)($_REQUEST['p_cbsid']),
									'title'  =>  $new_title,
									'is_important'  =>  trim($_REQUEST['new_important'])
								);
			
			$url_info = $this->faq_new_c->get_url_info($page, 'list_s', $page_size,$this->webType, $search_array);
			$new_info = $this->faq_new_c->get_new_info('list_s', $url_info['page_info']['start'], $page_size, $this->webType, $search_array);
		} else {
			$url_info = $this->faq_new_c->get_url_info($page, 'list', $page_size, $this->webType);
			$new_info = $this->faq_new_c->get_new_info('list', $url_info['page_info']['start'], $page_size, $this->webType);
		}
		
		
		$category_info = $this->faq_new_c->get_faq_category_list();
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		$GLOBALS['smarty']->assign('faqInfo',   $temInfo['faq_info']);
		$GLOBALS['smarty']->assign('orderInfo',   $temInfo['order_info']);
		$GLOBALS['smarty']->assign('web_type',   $this->webType);
		$GLOBALS['smarty']->assign('category_info',   $category_info);
		$GLOBALS['smarty']->assign('p_cbsid',   trim($_REQUEST['p_cbsid']));
		$GLOBALS['smarty']->assign('new_id',   trim($_REQUEST['new_id']));
		$GLOBALS['smarty']->assign('new_title',   $new_title);
		$GLOBALS['smarty']->assign('new_important',   trim($_REQUEST['new_important']));
		$GLOBALS['smarty']->assign('new_status',   trim($_REQUEST['new_status']));
		$GLOBALS['smarty']->assign('new_reason_id',   trim($_REQUEST['new_reason_id']));
		$GLOBALS['smarty']->assign('new_type',   trim($_REQUEST['new_type']));
		$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
		$GLOBALS['smarty']->assign('new_info',$new_info );
		$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
		$GLOBALS['smarty']->display('faq_new_index.htm'); 
	}
	
	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	public function edit() {	
		
		if ( $_REQUEST['act'] == 'edit' && !empty($_REQUEST['new_id']) ) { 
			
			$new_info = $this->faq_new_c->get_new_id_info((int)$_REQUEST['new_id']);
			
			$GLOBALS['smarty']->assign('chr_13',     chr(13));
			$GLOBALS['smarty']->assign('web_type',   $this->webType);
			$GLOBALS['smarty']->assign('new_info',$new_info);
			$GLOBALS['smarty']->display('faq_new_info.htm'); 	
		}
	}
	
	/**
	* 弹出框显示
	* @param  NULL
	* @return  string
	*/
	public function category_list() {
		
		$category__info = $this->faq_new_c->get_index_category();
		$category_listdata = !empty($_REQUEST['category_str_list']) ? explode(',', $_REQUEST['category_str_list']) : array();
		$GLOBALS['smarty']->assign('category_listdata',$category_listdata);
		$GLOBALS['smarty']->assign('category__info',$category__info);
		$GLOBALS['smarty']->display('faq_category_list.htm'); 
	}
	
	/**
	* 处理相关的ajax
	* @param  NULL
	* @return  string
	*/
	function  process_ajax() {
	
		switch ($_GET['type']) {
			
			case 'new_recommend'://修改是否推荐首页
				if (!empty($_GET['id'])) {
					$this->faq_new_c->change_info((int) $_GET['id'], $_GET['value'], 'is_recommend_index', $_GET['type'], $this->webType );
				}
			break;
				
			case 'new_important'://修改是否重要
				if (!empty($_GET['id'])) {
					$this->faq_new_c->change_info((int) $_GET['id'], $_GET['value'], 'is_important', $_GET['type'], $this->webType );
				}
			break;
				
			case 'new_sort'://修改排序
				if (!empty($_GET['id'])) {
					$this->faq_new_c->change_info((int) $_GET['id'], $_GET['value'], 'new_sort', $_GET['type'], $this->webType );
				}
			break;
		}
	}
	
	/**
	* 生成order——Support文章模板
	* @param  NULL
	* @return  string
	*/
	function generate_order() {
	
		$sql = "SELECT faq_type.type_name, faq_info_new.sub_title,faq_info_new.title, faq_info_new.description, faq_info_new.keywords, faq_info_new.id 
				FROM faq_info_new, faq_type 
				WHERE faq_info_new.type_id = faq_type.type_id AND FIND_IN_SET( faq_info_new.type_id, '1,2,4,7,8' ) AND faq_info_new.web_type = ".$this->webType;
		$result = $GLOBALS['db']->query($sql);
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		$orderInfo =  $temInfo['order_info'];
		
		$GLOBALS['db']->query( 'LOCK TABLES Table_'.$orderInfo['table'].' WRITE');//锁表
		
		$sql  = 'UPDATE Table_'.$orderInfo['table'].' SET is_delete = 2 ';
		$GLOBALS['db']->query($sql);  
		
		while ($row = $GLOBALS['db']->fetch_array($result)) {
			
			$array_where = array('tables' => 'Table_'.$orderInfo['table'],
								'tem_where' => ' WHERE Field_'.$orderInfo['temKey'].' = '.$row['id'],
								'replace_value' => $row['sub_title'],
								'type_name' => $this->replace_str($row['type_name']),
								'data_id' => $row['id']);
			$array_date  = $this->get_template_url($array_where);
			$count = $array_date['count'];
			$url = $array_date['url'];
			
			//表示在模板中不存在
			if ($count == 0) {
				$sql = "INSERT INTO Table_{$orderInfo['table']} ( `url`,`CreatorID`,`MenderID`,`MendTime`,`page_num`, 
						Field_{$orderInfo['temKey']},Field_{$orderInfo['temTitle']},Field_{$orderInfo['temKeywords']},
						Field_{$orderInfo['temDescription']},Field_{$orderInfo['baseTitle']} ) 
						VALUES('{$url}','1','1',UNIX_TIMESTAMP(),'1','{$row['id']}','".addslashes($row['title'])."', 
						'".addslashes($row['keywords'])."','".addslashes($row['description'])."','".addslashes($row['title'])."')";
				
				
			} else {
				
				$sql  = "UPDATE Table_{$orderInfo['table']} SET 	CreatorID = '1',MenderID = '1',MendTime = UNIX_TIMESTAMP() ,
						page_num = '1',Field_{$orderInfo['temTitle']} = '".addslashes($row['title'])."' ,
						Field_{$orderInfo['temKeywords']} = '".addslashes($row['keywords'])."' , 
						Field_{$orderInfo['temDescription']} = '".addslashes($row['description'])."' ,Field_{$orderInfo['baseTitle']} = '".addslashes($row['title'])."' 
						WHERE Field_{$orderInfo['temKey']} = '{$row['id']}' ";
			}
			
			$GLOBALS['db']->query($sql);
			
			$sql  = "UPDATE Table_{$orderInfo['table']} SET is_delete = 0 
					WHERE Field_{$orderInfo['temKey']} = '{$row['id']}' ";
			$GLOBALS['db']->query($sql);
		}
		
		$sql  = "SELECT url 
				FROM Table_{$orderInfo['table']} 
				WHERE is_delete = 2 ";
		$this->del_template($sql);
		
		$sql  = "DELETE 
				FROM Table_{$orderInfo['table']} 
				WHERE is_delete = 2 ";
		$GLOBALS['db']->query($sql);
		
		$GLOBALS['db']->query( "UNLOCK TABLES");
		
		unset($temInfo);
		
		if ($_GET['syn_fage'] != 1) {
		
			echo  '<script type="text/javascript"> 
			alert("生成order——Support文章模板成功！");
			window.location.href="http://cms.wondershare.com.br.wx/wondershare/faq_new.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	}
	
	/**
	* 生成产品相关文章模板（产品在线帮助）
	* @param  NULL
	* @return  string
	*/
	function product_help() {
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		
		//生成产品在线帮助文章
		$sql = "SELECT * 
				FROM (
				    SELECT online_help.sub_title,online_help.title,online_help.description ,won_product.id as won_id, 
				    online_help.keywords,product.product_name,product.product_cbsid,product.product_brand,online_help.id as help_id 
				    FROM online_help, won_product, product, version 
				    WHERE won_product.won_product_id = product.product_id AND product.product_id = version.product_id AND 
				    FIND_IN_SET( version.version_id, online_help.version_id ) AND online_help.is_video =0 {$temInfo['where_str']} 
				    AND won_product.won_product_delete = 0 AND online_help.web_type = '{$this->webType}'
				    ORDER BY version.version_name DESC , help_id DESC
				)  AS help 
				GROUP BY won_id ";
		$result = $GLOBALS['db']->query($sql);

		$GLOBALS['db']->query( "LOCK TABLES Table_{$temInfo['products_help_info']['table']} WRITE");//锁表
		
		$sql  = "UPDATE Table_{$temInfo['products_help_info']['table']} SET is_delete = 2 ";
		$GLOBALS['db']->query($sql);  
		
		while ($row = $GLOBALS['db']->fetch_array($result)) {
			
			$array_where = array(
								'tables' => 'Table_'.$temInfo['products_help_info']['table'],
								'tem_where' => ' WHERE Field_'.$temInfo['products_help_info']['temHelpId'].' = "'.$row['help_id'].'" AND Field_'.$temInfo['products_help_info']['pId'].' = '.$row['won_id'],
								'replace_value' => $row['sub_title'],'product_name' => trim(str_replace($row['product_brand'],'',$row['product_name'])),
								'data_id' => $row['help_id'] 
			);
			
			$array_date  = $this->get_template_url($array_where);
			$count = $array_date['count'];
			$url = $array_date['url'];
			
			//表示在模板中不存在
			if ($count == 0)
			{
				$sql = "INSERT INTO Table_{$temInfo['products_help_info']['table']} ( `url`,`CreatorID`,`MenderID`,`MendTime`,`page_num`, 
						Field_{$temInfo['products_help_info']['pId']},Field_{$temInfo['products_help_info']['pName']},
						Field_{$temInfo['products_help_info']['temHelpType']}, Field_{$temInfo['products_help_info']['temHelpId']},
						Field_{$temInfo['products_help_info']['temHelpTitle']},Field_{$temInfo['products_help_info']['temTitle']}, 
						Field_{$temInfo['products_help_info']['temKeywords']},Field_{$temInfo['products_help_info']['temDescription']},
						Field_{$temInfo['products_help_info']['pCbsId']} ) 
 						VALUES ('{$url}','1','1',UNIX_TIMESTAMP(),'1','{$row['won_id']}','".addslashes($row['product_name'])."', 
 						'在线帮助文章','{$row['help_id']}','".addslashes($row['title'])."','Get Started with {$row['product_name']}',
 						'".addslashes($row['keywords'])."' ,'".addslashes($row['description'])."','{$row['product_cbsid']}')";
				
			} else {
				
				$sql =  "UPDATE Table_{$temInfo['products_help_info']['table']} SET CreatorID = '1',MenderID = '1',
						MendTime = UNIX_TIMESTAMP(), page_num = '1',Field_{$temInfo['products_help_info']['pName']} = '".addslashes($row['product_name'])."',
						Field_{$temInfo['products_help_info']['temHelpType']} = '在线帮助文章', Field_{$temInfo['products_help_info']['temHelpTitle']} = '".addslashes($row['title'])."',
						Field_{$temInfo['products_help_info']['temTitle']} = 'Get Started with {$row['product_name']}', 
						Field_{$temInfo['products_help_info']['temDescription']} = '".addslashes($row['description'])."' ,
						Field_{$temInfo['products_help_info']['temKeywords']} = '".addslashes($row['keywords'])."' 
						WHERE Field_{$temInfo['products_help_info']['temHelpId']} = '{$row['help_id']}' AND Field_{$temInfo['products_help_info']['pId']} = '{$row['won_id']}' ";

			}
			$GLOBALS['db']->query($sql);
			
			$sql  = "UPDATE Table_{$temInfo['products_help_info']['table']} SET is_delete = 0 
					WHERE Field_{$temInfo['products_help_info']['temHelpId']} = '{$row['help_id']}' 
					AND Field_{$temInfo['products_help_info']['pId']} = '{$row['won_id']}' ";
			$GLOBALS['db']->query($sql);
		}
		
		$sql  = "DELETE FROM Table_{$temInfo['products_help_info']['table']} WHERE  is_delete = 2 ";
		$GLOBALS['db']->query($sql);
		
		$GLOBALS['db']->query( "UNLOCK TABLES");
	
	}
	
	/**
	* 生成产品FAQ相关文章模板
	* @param  array  $array_where 条件
	* @return  string
	*/
	function get_template_url($array_where) {
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		
		$sql =  'SELECT COUNT(*) as count FROM '.$array_where['tables'].$array_where['tem_where'];
		$count = $GLOBALS['db']->getOne($sql);
		
		$url = '';
		if ($count == 0) {
			
			$replace_value = $this->replace_str($array_where['replace_value']);
			$url_prefix = '/'.$temInfo['webDomain'].'/httpdocs/';
			
			if ($array_where['tables'] == 'Table_'.$temInfo['products_help_info']['table']) {
				
				$product_name_url = $this->replace_str($array_where['product_name']);
				$url = $url_prefix.$product_name_url.'/'.$replace_value.'.html';
				$url_middle = $product_name_url;
				
			} elseif ($array_where['tables'] == 'Table_'.$temInfo['faq_info']['table']) {
			
				$url = $url_prefix.'how-tos/'.$replace_value.'.html';
				$url_middle = 'how-tos/';
				
			} elseif($array_where['tables'] == 'Table_'.$temInfo['order_info']['table']) {
				
				$url = $url_prefix.$array_where['type_name'].'/'.$replace_value.'.html';
				$url_middle = $array_where['type_name'].'/';
			}
			
			$sql =  'SELECT COUNT(*) as count FROM '.$array_where['tables'].' WHERE url = "'.$url.'"';
			$nums = $GLOBALS['db']->getOne($sql);
			
			if ($nums > 0) {
				$url = $url_prefix.$url_middle.$replace_value.'-'.$array_where['data_id'].'.html';
			}
			
			$url =  str_replace('-.html','.html',$url);
		}
		
		return array('url' => $url, 'count' => $count);
	
	}
	
	/**
	* 生成产品FAQ相关文章模板
	* @param  NULL
	* @return  string
	*/
	function product_faq() {
		
		$sql = "SELECT sub_title,title,description ,keywords,id 
				FROM faq_info_new 
				WHERE version_id != '' AND `type_id` = 6 AND web_type = '{$this->webType}' ";
		$result = $GLOBALS['db']->query($sql);
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		$faqInfo =  $temInfo['faq_info'];
		
		$GLOBALS['db']->query( 'LOCK TABLES  Table_'.$faqInfo['table'].' WRITE');//锁表
		
		$sql  = 'UPDATE Table_'.$faqInfo['table'].' SET is_delete = 2 ';
		$GLOBALS['db']->query($sql);  
		
		while ($row = $GLOBALS['db']->fetch_array($result)) {
			
			$array_where = array(
							'tables' => 'Table_'.$faqInfo['table'],
							'tem_where' => ' WHERE Field_'.$faqInfo['temKey'].' = '.$row['id'],
							'replace_value' => $row['sub_title'],
							'data_id' => $row['id'] 
			);
			$array_date  = $this->get_template_url($array_where);
			
			$count = $array_date['count'];
			$url = $array_date['url'];
			
			//表示在模板中不存在
			if ($count == 0) {
				
				$sql = "INSERT INTO Table_{$faqInfo['table']} ( `url`,`CreatorID`,`MenderID`,`MendTime`,`page_num`, 
						Field_{$faqInfo['temKey']},Field_{$faqInfo['baseTitle']},Field_{$faqInfo['temTitle']},
						Field_{$faqInfo['temKeywords']},Field_{$faqInfo['temDescription']} ) 
						VALUES ('{$url}','1','1',UNIX_TIMESTAMP(),'1','{$row['id']}','".addslashes($row['title'])."', 
						'".addslashes($row['title'])."| {$temInfo['product_brand']} Support','".addslashes($row['keywords'])."',
						'".addslashes($row['description'])."')";
		
			
			} else {
				
				$sql  = "UPDATE Table_{$faqInfo['table']} SET CreatorID = '1',MenderID = '1',MendTime = UNIX_TIMESTAMP() ,
						page_num = '1',Field_{$faqInfo['baseTitle']} = '".addslashes($row['title'])."' ,
						Field_{$faqInfo['temTitle']} = '".addslashes($row['title'])."| {$temInfo['product_brand']} Support',
						Field_{$faqInfo['temKeywords']} = '".addslashes($row['keywords'])."' ,Field_{$faqInfo['temDescription']} = '".addslashes($row['description'])."' 
						WHERE Field_{$faqInfo['temKey']} = '{$row['id']}' ";
				
				
			}
			
			$GLOBALS['db']->query($sql);
			
			$sql  = 'UPDATE Table_'.$faqInfo['table'].' SET is_delete = 0  WHERE Field_'.$faqInfo['temKey'].' = '.$row['id'];
			$GLOBALS['db']->query($sql);
			
		} 
		//删除模板处理(删除文件以及数据库记录)
		$sql  = 'SELECT url FROM Table_'.$faqInfo['table'].' WHERE  is_delete = 2 ';
		$this->del_template($sql);
		
		$sql  = 'DELETE FROM Table_'.$faqInfo['table'].' WHERE  is_delete = 2 ';
		$GLOBALS['db']->query($sql);
		
		$GLOBALS['db']->query( "UNLOCK TABLES");
		
		unset($temInfo);
		
		if ($_GET['syn_fage'] != 1) {
			
			echo  '<script type="text/javascript"> 
			alert("生成产品相关FAQ文章模板成功！");
			window.location.href="http://cms.wondershare.com.br.wx/wondershare/faq_new.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	}

	/**
	* 生成产品相关模板
	* @param  NULL
	* @return  string
	*/
	function product_related() {
		
		$table_howtos_indert = $table_video_indert = $table_history_indert = $table_suggestion_indert = '';
		$table_howtos_public = $table_video_public = $table_history_public = $table_suggestion_public = '';
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		
		
		$sql  = 'UPDATE Table_'.$temInfo['products_info']['table'].' SET is_delete = 2 ';
		$GLOBALS['db']->query($sql);
		
		$sql = "SELECT won_product.id ,product.product_name,product.product_cbsid,product.product_id,product.product_brand ,product.product_variant 
				FROM won_product,product 
				WHERE product.product_id = won_product.won_product_id AND won_product.won_product_delete = 0 {$temInfo['where_str']} ";
		$result = $GLOBALS['db']->query($sql);
		
		while ($row = $GLOBALS['db']->fetch_array($result)) {
			
			$product_name_url =  $url = '';
			//得到木有品牌的产品名称
			$row['product_n_brand'] = trim(str_replace($row['product_brand'], '', $row['product_name']));   
			
			$product_title = empty($row['product_variant']) ? $row['product_name'] : $row['product_brand'].' '.$row['product_variant'];
			
			//下面是生成单个产品帮助首页
			$sql = "SELECT url 
					FROM Table_{$temInfo['products_info']['table']} 
					WHERE Field_{$temInfo['products_info']['pId']} = '{$row['id']}' ";
			$url = $GLOBALS['db']->getOne($sql);
			
			if (empty($url)) {
				
				$product_name_url  = $this->replace_str($row['product_n_brand']);
				$url = '/'.$temInfo['webDomain'].'/httpdocs/'.$product_name_url.'/index.html';
				$url =  str_replace('-.html', '.html', $url);
				$sql = "INSERT INTO Table_{$temInfo['products_info']['table']} ( `url`,`CreatorID`,`MenderID`,`MendTime`,`page_num`, Field_{$temInfo['products_info']['pId']},
						Field_{$temInfo['products_info']['pName']},Field_{$temInfo['products_info']['temTitle']}, 
						Field_{$temInfo['products_info']['temKeywords']},Field_{$temInfo['products_info']['temDescription']},
						Field_{$temInfo['products_info']['pCbsId']} ) 
						VALUES ('{$url}','1','1',UNIX_TIMESTAMP(),'1','{$row['id']}','".addslashes($row['product_name'])."', 
						'".addslashes(trim($product_title))." Technical Support & FAQs','".addslashes($row['product_n_brand'])." support', 'Online help 
						for ".addslashes($row['product_n_brand']).", including frequently asked questions, user guide and product suggestion.', '{$row['product_cbsid']}')";
										
				
			} else {
				
				$product_name_url = str_replace(array('/index.html', '/'.$temInfo['webDomain'].'/httpdocs/'), array('', ''), $url);
				$sql = "UPDATE Table_{$temInfo['products_info']['table']} SET CreatorID = 1 ,MenderID = 1,MendTime = UNIX_TIMESTAMP() ,page_num = 1 ,
						Field_{$temInfo['products_info']['pName']} = '".addslashes($row['product_name'])."' ,
						Field_{$temInfo['products_info']['temTitle']} = '".addslashes(trim($product_title))." Technical Support & FAQs' ,
						Field_{$temInfo['products_info']['temKeywords']} = '".addslashes($row['product_n_brand'])." support' , 
						Field_{$temInfo['products_info']['temDescription']} = 'Online help 
						for ".addslashes($row['product_n_brand']).", including frequently asked questions, user guide and product suggestion.', 
						Field_{$temInfo['products_info']['pCbsId']} = '{$row['product_cbsid']}' 
						WHERE Field_{$temInfo['products_info']['pId']} = '{$row['id']}' ";
				
			}
			$GLOBALS['db']->query($sql);
			
			//设置为不删除状态
			$sql  = "UPDATE Table_{$temInfo['products_info']['table']} SET is_delete = 0  
					WHERE Field_{$temInfo['products_info']['pId']} = '{$row['id']}'";
			$GLOBALS['db']->query($sql);
			
	
			
			//下面是生成单个产品产品的FAQ
			$sql = "SELECT version_id FROM version WHERE product_id = '{$row['product_id']}' ";
			$result_other = $GLOBALS['db']->query($sql);
			
			$fage = 0; //用于判断标志
			while ($row_other = $GLOBALS['db']->fetch_array($result_other)) {
				
				if ($fage >= 1)  {
					break;
				}
				
				$sql  = "SELECT id 
						FROM faq_info_new 
						WHERE FIND_IN_SET( '{$row_other['version_id']}',version_id) AND type_id = 6 ";
				$nums = $GLOBALS['db']->num_rows($GLOBALS['db']->query($sql));
				
				if ($nums > 0) {
					
					$url = '/'.$temInfo['webDomain'].'/httpdocs/'.$product_name_url.'/how-tos/index.html';
					$table_howtos_public = " ('{$url}','1','1',UNIX_TIMESTAMP(),'1','{$row['id']}','".addslashes($row['product_name'])."',
											'How-tos & Toubleshooting of ".addslashes($row['product_n_brand'])."','how-tos, troubleshooting',
											'How-tos & Toubleshooting of ".addslashes($row['product_n_brand']).",guide you to use Video Converter',
											'{$row['product_cbsid']}') ";
					
					if ( empty($table_howtos_indert)) {
						
						$table_howtos_indert .= "INSERT INTO  Table_{$temInfo['products_howtos_info']['table']}
						 (
						 `url`,`CreatorID`,`MenderID`,`MendTime`,`page_num`,
						 Field_{$temInfo['products_howtos_info']['pId']},Field_{$temInfo['products_howtos_info']['pName']}
						 ,Field_{$temInfo['products_howtos_info']['temTitle']},Field_{$temInfo['products_howtos_info']['temKeywords']}
						 ,Field_{$temInfo['products_howtos_info']['temDescription']},Field_{$temInfo['products_howtos_info']['pCbsId']}
						 ) VALUES ".$table_howtos_public;
						
					} else {
						$table_howtos_indert .= ' , '.$table_howtos_public;
					}
					
					$fage++;
				}
			}

			//下面是生成单个产品History
			$url = '/'.$temInfo['webDomain'].'/httpdocs/'.$product_name_url.'/history.html'; 
			
			$table_history_public = " ('{$url}','1','1',UNIX_TIMESTAMP(),'1','{$row['id']}','".addslashes($row['product_name'])."',
									'".addslashes($row['product_n_brand'])." Revision History| {$temInfo['product_brand']} Support','Revision History,".addslashes($row['product_n_brand'])."',
									'".addslashes($row['product_n_brand'])."Revision History,help you to learn about what\'s new with".addslashes($row['product_n_brand'])."',
									'{$row['product_cbsid']}' ) ";
			
			if (empty($table_history_indert)) {
				
				$table_history_indert .= "INSERT INTO  Table_{$temInfo['products_history_info']['table']}
				(
				`url`,`CreatorID`,`MenderID`,`MendTime`,`page_num`,
				Field_{$temInfo['products_history_info']['pId']},Field_{$temInfo['products_history_info']['pName']},
				Field_{$temInfo['products_history_info']['temTitle']},Field_{$temInfo['products_history_info']['temKeywords']}
				,Field_{$temInfo['products_history_info']['temDescription']},Field_{$temInfo['products_history_info']['pCbsId']}
				) VALUES ".$table_history_public;
				
			} else {
				$table_history_indert .= ' , '.$table_history_public;
			}
			
	
			//下面是生成单个产品-Suggestion
			$url = '/'.$temInfo['webDomain'].'/httpdocs/'.$product_name_url.'/suggestion.html'; 
			$table_suggestion_public = "('{$url}','1','1',UNIX_TIMESTAMP(),'1','{$row['id']}','".addslashes($row['product_name'])."',
										'Submite a Suggestion for ".addslashes($row['product_n_brand'])."  | {$temInfo['product_brand']} Products','suggestion',
										'Submite a Suggestion for ".addslashes($row['product_n_brand']).", help us improve.','{$row['product_cbsid']}')";
			
			if (empty($table_suggestion_indert)) {
				
				$table_suggestion_indert .= "INSERT INTO  Table_{$temInfo['products_suggestion_info']['table']}
				(
				`url`,`CreatorID`,`MenderID`,`MendTime`,`page_num`,
				Field_{$temInfo['products_suggestion_info']['pId']},Field_{$temInfo['products_suggestion_info']['pName']},
				Field_{$temInfo['products_suggestion_info']['temTitle']},Field_{$temInfo['products_suggestion_info']['temKeywords']}
				,Field_{$temInfo['products_suggestion_info']['temDescription']},Field_{$temInfo['products_suggestion_info']['pCbsId']}
				) VALUES  " . $table_suggestion_public;
			
			} else {
				$table_suggestion_indert .= ' , '.$table_suggestion_public;
			}
		}
	
		
		//删除模板处理
		$sql  = "SELECT url,Field_{$temInfo['products_info']['pId']}
				FROM Table_{$temInfo['products_info']['table']}
				WHERE is_delete = 2 ";
		$result = $GLOBALS['db']->query($sql);
		
		while ($row = $GLOBALS['db']->fetch_array($result)) {
		
			$sql = "SELECT url 
					FROM Table_{$temInfo['products_info']['table']}
					WHERE Field_{$temInfo['products_info']['pId']} = '{$row['Field_'.$temInfo['products_info']['pId']]}' ";
			$this->del_template($sql, 'del_directory');
		}
		//以下为清空表
		$GLOBALS['db']->query('TRUNCATE TABLE  Table_'.$temInfo['products_howtos_info']['table']);//清空单个产品pro-How-tos(产品的FAQ)

		$GLOBALS['db']->query('TRUNCATE TABLE  Table_'.$temInfo['products_history_info']['table']);//清空单个产品History
		$GLOBALS['db']->query('TRUNCATE TABLE  Table_'.$temInfo['products_suggestion_info']['table']);//清空单个产品-Suggestion
		//以下为批量插入记录
		!empty($table_howtos_indert) ? $GLOBALS['db']->query($table_howtos_indert) : '';

		!empty($table_history_indert) ? $GLOBALS['db']->query($table_history_indert) : '';
		!empty($table_suggestion_indert) ? $GLOBALS['db']->query($table_suggestion_indert) : '';
		
		$sql  = 'DELETE FROM Table_'.$temInfo['products_info']['table'].' WHERE  is_delete = 2 ';
		$GLOBALS['db']->query($sql);
		
		unset($table_howtos_indert, $table_history_indert, $table_suggestion_indert, $table_howtos_public, $table_history_public, $table_suggestion_public);
		unset($row, $row_other, $temInfo);
		
		$this->product_help();
		
		if ($_GET['syn_fage'] != 1 ) {
		
			echo  '<script type="text/javascript"> 
			alert("生成产品相关模板成功！");
			window.location.href="http://cms.wondershare.com.br.wx/wondershare/faq_online_help.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	} 
}

$action    = $_REQUEST['action']; 
$faq_comment   = new Faq_New;
if (method_exists($faq_comment, $action)) {
	$faq_comment->$action();
} else {
	$faq_comment->index();
}

?>
